package com.gs.SynAntiWandering.util;

import android.content.Context;
import android.content.SharedPreferences;

public class SPUtil {
    private static volatile SPUtil instance;
    private final SharedPreferences share;
    private final SharedPreferences.Editor editor;
    private final String SHARED_NAME = "HKPC";

    public SPUtil() {
        share = AppUtil.getInstance().getContext().getSharedPreferences(SHARED_NAME, Context.MODE_MULTI_PROCESS);
        editor = share.edit();
    }

    public static SPUtil getInstance() {
        if (instance == null) {
            synchronized (SPUtil.class) {
                if (instance == null) {
                    instance = new SPUtil();
                }
            }
        }
        return instance;
    }

    /**
     * 切换语言时，其他进程要重新获取SharedPreferences
     */
    public void refresh() {
        instance = null;
    }

    public void putString(String spName, String value) {
        editor.putString(spName, value);
        editor.commit();
    }

    public String getString(String spName) {
        return share.getString(spName, "");
    }

    public void putInt(String spName, int value) {
        editor.putInt(spName, value);
        editor.commit();
    }

    public int getInt(String spName, int defaultValue) {
        return share.getInt(spName, defaultValue);
    }

}
